package com.jic.point.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.point.constant.consist.ResponseCode;
import com.jic.point.model.entity.ConfPointCardApply;
import com.jic.point.model.entity.ConfPointCardBatch;
import com.jic.point.model.vo.request.ConfPointCardApplyRequest;
import com.jic.point.model.vo.request.ConfPointCardBatchRequest;
import com.jic.point.model.vo.request.QuantityRequest;
import com.jic.point.model.vo.response.ConfPointCardApplyResponse;
import com.jic.point.model.vo.response.ConfPointCardBatchResponse;
import com.jic.point.service.ConfPointCardApplyService;
import com.jic.point.service.ConfPointCardBatchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("confPointCardBatch")
public class ConfPointCardBatchController {

    @Resource
    private ConfPointCardBatchService service;

    /**
     * 删除记录
     *
     * @param request 删除[积分卡批量开卡申请表]请求参数
     * @return ConfPointCardApplyResponse 删除[积分卡批量开卡申请表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[积分卡批量开卡申请表]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody ConfPointCardBatchRequest request) throws Throwable {
        ConfPointCardBatch confPointCardApply = new ConfPointCardBatch();
        BeanUtils.copyProperties(request, confPointCardApply);
        long flag = service.deleteByPrimaryKeyLogically(confPointCardApply);
        return RestResult.success(flag);
    }


    @ApiOperation(value = "保存或更新[积分卡批量开卡申请表]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody ConfPointCardBatchRequest request) throws Throwable {
        ConfPointCardBatch confPointCardApply = new ConfPointCardBatch();
        BeanUtils.copyProperties(request, confPointCardApply);
        long flag = service.saveOrUpdate(confPointCardApply);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     *
     * @param request 修改[积分卡批量开卡申请表]请求参数
     * @return ConfPointCardApplyResponse 修改[积分卡批量开卡申请表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[积分卡批量开卡申请表]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody ConfPointCardBatchRequest request) throws Throwable {
        ConfPointCardBatch confPointCardApply = new ConfPointCardBatch();
        BeanUtils.copyProperties(request, confPointCardApply);
        long flag = service.updateByPrimaryKeySelective(confPointCardApply);
        return RestResult.success(flag);
    }


    /**
     * 单表查询详情信息
     *
     * @param request 查询[积分卡批量开卡申请表]详情请求参数
     * @return ConfPointCardApplyResponse 查询[积分卡批量开卡申请表]详情响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[积分卡批量开卡申请表]详情")
    @ResponseBody
    public RestResult<ConfPointCardApplyResponse> getByPrimaryKey(@Validated @RequestBody ConfPointCardBatchRequest request) {
        if (request != null && request.getId() != null) {
            ConfPointCardBatch confPointCardApply = new ConfPointCardBatch();
            BeanUtils.copyProperties(request, confPointCardApply);
            ConfPointCardBatch byPrimaryKey = service.getByPrimaryKey(confPointCardApply);
            if (byPrimaryKey == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            ConfPointCardApplyResponse bo = new ConfPointCardApplyResponse();
            BeanUtils.copyProperties(byPrimaryKey, bo);
            bo.setQuantity(JSONObject.parseArray(byPrimaryKey.getQuantity(),QuantityRequest.class));
            return RestResult.success(bo);
        }
        return RestResult.success(new ConfPointCardApplyResponse());
    }

    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listPage")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageInfo<ConfPointCardBatchResponse>> listPage(@RequestBody ConfPointCardBatchRequest request) throws Throwable {
        return RestResult.success(service.listPageBySelective(request, request.getPageNum(), request.getPageSize()));
    }


    /**
     * CONF_POINT_CARD_APPLY 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [积分卡批量开卡申请表] ")
    @ResponseBody
    public RestResult<List<ConfPointCardBatchResponse>> listConfPointCardApplyAllRecord(@RequestBody ConfPointCardBatchRequest request) throws Throwable {
        ConfPointCardBatch param = new ConfPointCardBatch();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        List<ConfPointCardBatch> list = service.listAllRecord(param);
        List<ConfPointCardBatchResponse> responseList = list.stream().map(temp -> {
            ConfPointCardBatchResponse bo = new ConfPointCardBatchResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

    /**
     *
     *
     * @description: 释放积分卡
     * @return: 
     * @author: fengrui
     * @time: 2020/11/22 15:17
     */  
    @PostMapping("releasePointCart")
    @ApiOperation("释放积分卡")
    @ResponseBody
    public RestResult<Boolean> releasePointCart(@RequestBody ConfPointCardBatchRequest request){
        Integer result = service.releasePointCart(request.getReleaseBatch());
        return RestResult.success(result>0);
    }


        @PostMapping("count")
    @ApiOperation("总条数")
    @ResponseBody
    public RestResult<List<ConfPointCardBatchResponse>> count(){
        List<ConfPointCardBatchResponse> list = service.count();
        if(list==null||list.size()<1){
            return RestResult.error("6666","出错了");
        }
        return RestResult.success(list);
    }
    
}
